//
//	DigestCalculationTest.m
//	Tests
//
//	Created by Joey Deng on 1/28/20.
//

#import <XCTest/XCTest.h>
#import "dnssec_v2_crypto.h"
#include "mDNSEmbeddedAPI.h"
#if MDNSRESPONDER_SUPPORTS(APPLE, DNSSECv2)

@interface DigestCalculationTest : XCTestCase

@end

@implementation DigestCalculationTest

- (void)testSHA1 {
	char * const inputs[] = {
		"abc",
		"",
		"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
		"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
		"one million repetitions of 'a'"
	};

	unsigned char outputs[][SHA1_OUTPUT_SIZE] = {
		{0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d},
		{0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09},
		{0x84, 0x98, 0x3e, 0x44, 0x1c, 0x3b, 0xd2, 0x6e, 0xba, 0xae, 0x4a, 0xa1, 0xf9, 0x51, 0x29, 0xe5, 0xe5, 0x46, 0x70, 0xf1},
		{0xa4, 0x9b, 0x24, 0x46, 0xa0, 0x2c, 0x64, 0x5b, 0xf4, 0x19, 0xf9, 0x95, 0xb6, 0x70, 0x91, 0x25, 0x3a, 0x04, 0xa2, 0x59},
		{0x34, 0xaa, 0x97, 0x3c, 0xd4, 0xc4, 0xda, 0xa4, 0xf6, 0x1e, 0xeb, 0x2b, 0xdb, 0xad, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6f}
	};
	unsigned char	actual_output[SHA1_OUTPUT_SIZE];

	XCTAssertEqual(sizeof(inputs) / sizeof(char *), sizeof(outputs) / SHA1_OUTPUT_SIZE);

	for (int i = 0, limit = sizeof(inputs) / sizeof(char *); i < limit; i++) {
		char *						input;
		const unsigned char * const expected_output = outputs[i];

		if (i < 4) {
			input = inputs[i];
		} else if (i == 4) {
			input = malloc(1000001);
			memset(input, 'a', 1000000);
			input[1000000] = '\0';
		} else {
			XCTAssertTrue(0);
		}

		uint8_t calculated = calculate_digest_for_data((const unsigned char *)input, strlen(input), DIGEST_SHA_1,
			actual_output, sizeof(actual_output));
		XCTAssertTrue(calculated);

		XCTAssertTrue(memcmp(actual_output, expected_output, SHA1_OUTPUT_SIZE) == 0);
	}
}

- (void)testSHA256 {
	char * const inputs[] = {
		"abc",
		"",
		"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
		"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
		"one million repetitions of 'a'"
	};

	unsigned char outputs[][SHA256_OUTPUT_SIZE] = {
		{0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad},
		{0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55},
		{0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1},
		{0xcf, 0x5b, 0x16, 0xa7, 0x78, 0xaf, 0x83, 0x80, 0x03, 0x6c, 0xe5, 0x9e, 0x7b, 0x04, 0x92, 0x37, 0x0b, 0x24, 0x9b, 0x11, 0xe8, 0xf0, 0x7a, 0x51, 0xaf, 0xac, 0x45, 0x03, 0x7a, 0xfe, 0xe9, 0xd1},
		{0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92, 0x81, 0xa1, 0xc7, 0xe2, 0x84, 0xd7, 0x3e, 0x67, 0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97, 0x20, 0x0e, 0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0}
	};
	unsigned char	actual_output[SHA256_OUTPUT_SIZE];

	XCTAssertEqual(sizeof(inputs) / sizeof(char *), sizeof(outputs) / SHA256_OUTPUT_SIZE);

	for (int i = 0, limit = sizeof(inputs) / sizeof(char *); i < limit; i++) {
		char *						input;
		const unsigned char * const expected_output = outputs[i];

		if (i < 4) {
			input = inputs[i];
		} else if (i == 4) {
			input = malloc(1000001);
			memset(input, 'a', 1000000);
			input[1000000] = '\0';
		} else {
			XCTAssertTrue(0);
		}

		uint8_t caculated = calculate_digest_for_data((const unsigned char *)input, strlen(input), DIGEST_SHA_256,
									actual_output, sizeof(actual_output));
		XCTAssertTrue(caculated);

		XCTAssertTrue(memcmp(actual_output, expected_output, SHA256_OUTPUT_SIZE) == 0);
	}
}

- (void)testSHA384 {
	char * const inputs[] = {
		"abc",
		"",
		"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
		"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
		"one million repetitions of 'a'"
	};

	unsigned char outputs[][SHA384_OUTPUT_SIZE] = {
		{0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b, 0xb5, 0xa0,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0x9d, 0x0e, 0x18, 0x09, 0x71, 0x64, 0x74, 0xcb, 0x08, 0x6e, 0x83, 0x4e, 0x31, 0x0a, 0x4a, 0x1c, 0xed, 0x14, 0x9e, 0x9c, 0x00, 0xf2, 0x48, 0x52, 0x79, 0x72, 0xce, 0xc5, 0x70, 0x4c, 0x2a, 0x5b, 0x07, 0xb8, 0xb3, 0xdc, 0x38, 0xec, 0xc4, 0xeb, 0xae, 0x97, 0xdd, 0xd8, 0x7f, 0x3d, 0x89, 0x85}
	};
	unsigned char	actual_output[SHA384_OUTPUT_SIZE];

	XCTAssertEqual(sizeof(inputs) / sizeof(char *), sizeof(outputs) / SHA384_OUTPUT_SIZE);

	for (int i = 0, limit = sizeof(inputs) / sizeof(char *); i < limit; i++) {
		char *						input;
		const unsigned char * const expected_output = outputs[i];

		if (i < 4) {
			input = inputs[i];
		} else if (i == 4) {
			input = malloc(1000001);
			memset(input, 'a', 1000000);
			input[1000000] = '\0';
		} else {
			XCTAssertTrue(0);
		}

		uint8_t calculated =	calculate_digest_for_data((const unsigned char *)input, strlen(input), DIGEST_SHA_384,
			actual_output, sizeof(actual_output));
		XCTAssertTrue(calculated);

		XCTAssertTrue(memcmp(actual_output, expected_output, SHA384_OUTPUT_SIZE) == 0);
	}
}

@end
#endif // MDNSRESPONDER_SUPPORTS(APPLE, DNSSECv2)
